<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta
      http-equiv="X-UA-Compatible"
      content="IE=edge"
    />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0"
    />
    <title>Document</title>
    <link
      rel="stylesheet"
      href=""
    />
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      .page div {
        height: 100vh;
        text-align: center;
        border-bottom: 1px solid #ccc;
      }

      .page .nav {
        height: 50px;
        border: 1px solid #ccc;
      }

      .flat {
        position: fixed;
        top: 50%;
        transform: translateY(-50%);
        right: 20px;
        cursor: pointer;
      }

      .flat div {
        margin-bottom: 20px;
        padding: 10px 5px;
        border: 1px solid #ccc;
      }

      .flat .top {
        display: none;
      }

      .flat div.active {
        background-color: aqua;
        color: #fff;
      }
      html {
        scroll-behavior: smooth;
      }
    </style>
  </head>
  <body>
    <div class="page">
      <div class="nav"></div>
      <div class="banner"></div>
      <div class="goods goods-1">goods-1</div>
      <div class="goods goods-2">goods-2</div>
      <div class="goods goods-3">goods-3</div>
      <div class="goods goods-4">goods-4</div>
      <div class="goods goods-5">goods-5</div>
    </div>

    <div class="flat">
      <div class="goodsF goods-1-f">goods-1</div>
      <div class="goodsF goods-2-f">goods-2</div>
      <div class="goodsF goods-3-f">goods-3</div>
      <div class="goodsF goods-4-f">goods-4</div>
      <div class="goodsF goods-5-f">goods-5</div>
      <div class="top">回到顶部</div>
    </div>
    <script src=""></script>
    <script>
      const goods = document.querySelectorAll('.goods')
      const goodsFs = document.querySelectorAll('.goodsF')
      const topBtn = document.querySelector('.top')
      onscroll = () => {
        const tops = document.documentElement.scrollTop
        if (tops >= goods[0].offsetTop) {
          topBtn.style.display = 'block'
        } else {
          topBtn.style.display = 'none'
        }
        let activeIndex

        goods.forEach((el, i) => {
          if (tops >= el.offsetTop) {
            activeIndex = i
          }
        })
        document.querySelector('.active')?.classList.remove('active')
        goodsFs[activeIndex]?.classList.add('active')
      }

      goodsFs.forEach((el, i) => {
        el.addEventListener('click', () => {
          document.documentElement.scrollTop = goods[i].offsetTop
        })
      })

      topBtn.addEventListener('click', () => {
        document.documentElement.scrollTop = 0
      })
    </script>
  </body>
</html>
